package cc.sheji.controller;

import cc.sheji.Service.CommentService;
import cc.sheji.domain.*;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController

@RequestMapping(value = "/api/v1/comment")
public class CommentController {

    @Autowired
    CommentService commentService;


    @RequestMapping("/{id}")
    public JsonResult get(@PathVariable("id") Long id) throws Exception {
        return JsonResult.Success(commentService.get(id));
    }

    @PostMapping("")
    public JsonResult create(@RequestBody @Validated() Comment entity) {
        entity.setId(null);
        return JsonResult.Success(commentService.create(entity));
    }

    @PutMapping("/{id}")
    public JsonResult update(@PathVariable(name = "id") Long id, @RequestBody @Validated() Comment entity) {
        entity.setId(id);
        return JsonResult.Success(commentService.update(entity));
    }

    @DeleteMapping("")
    public JsonResult delete(@RequestBody @Validated() Comment entity) {

        return JsonResult.Success(commentService.delete(entity));
    }

    @GetMapping("/mine")
    public JsonResult mineList(CommentQueryCriteria criteria, Pageable pageable) throws InterruptedException {

        criteria.setUserId(StpUtil.getLoginIdAsLong());
        return JsonResult.Success(commentService.all(criteria, pageable));
    }

    @GetMapping("/mine1")
    public JsonResult mineListpc(CommentQueryCriteria criteria, Pageable pageable) throws InterruptedException {

        if(criteria.getQueryType()==1){
            criteria.setPUserId(StpUtil.getLoginIdAsLong());
            criteria.setParentId(0L);
            return JsonResult.Success(commentService.allPage(criteria, pageable));
        }else {
            criteria.setUserId(StpUtil.getLoginIdAsLong());
            return JsonResult.Success(commentService.all1(criteria, pageable));
        }
    }
    @GetMapping()
    public JsonResult list(CommentQueryCriteria criteria, Pageable pageable) throws InterruptedException {
        return JsonResult.Success(commentService.list(criteria, pageable));
    }

    @GetMapping("/all")
    public JsonResult all(CommentQueryCriteria criteria, Pageable pageable) throws InterruptedException {
        return JsonResult.Success(commentService.all(criteria, pageable));
    }

}
